<?php
class Controller_salessearchshow extends Controller_Admin
{
    public function before()
    {
        try {
            ACL::Required('salessearchshow');
        } catch ( HTTP_Exception_403 $e ) {
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect ( URL::base (true) );
        }

        parent::before();
    }

    public function action_index()
    {
        $this->title=__("SKU销量查询");
        $view = View::factory("salessearchshow");

        $_GET += array(
            'sales'   => 'ebay'
        );
        $sql = " FROM
            `{$_GET['sales']}_sales`
        WHERE
            `sku` <> ''";

        empty($_GET['minDate']) || $sql .= " AND `count_date` >= '{$_GET['minDate']}'";
        empty($_GET['maxDate']) || $sql .= " AND `count_date` <= '{$_GET['maxDate']}'";

        if(!empty($_GET['skuName'])) {
            if(!empty($_GET['skuLike'])) {
                $sql .= " AND `sku` LIKE '%{$_GET['skuName']}%' ";
            } else {
                $sql .= " AND `sku` = '{$_GET['skuName']}'";
            }
        }
        if( ($count = $this->request->param('id', false)) === false ) {                                                 //查询总长度
            $count = DB::query(Database::SELECT, 'SELECT COUNT(DISTINCT sku) c' . $sql)
                ->execute('newerp')->get('c', 0);
        }

        $sql = "SELECT
            `sku`, SUM(`sale`) `sum`
        {$sql} 
        GROUP BY 
            `sku`";

        empty($_GET['page']) && $_GET['page'] = 1;
        $_GET['page'] -= 1;
        $sql .= ' LIMIT ' . ($_GET['page'] * 50) . ', 50';

        $view->set('dataList',                                                                                      //数据列表
            DB::query(Database::SELECT, $sql)->execute('newerp')->as_array()
        );

        $pagination         = Pagination::factory(array(
          'items_per_page'  => 50, 
          'total_items'     => $count,
          'uri'             => '/salessearchshow/index/' . $count,
        ));
        $view->set('pagination', $pagination);                                                                      //分页列表

        $this->response->body($view);
    }

    /**
     * 描述 : 显示数据
     * 作者 : Edgar.lee
     */
    public function action_detailsData() {
        $sql = " FROM
            `{$_GET['sales']}_sales`
        WHERE
            `sku` <> ''";

        empty($_GET['minDate']) || $sql .= " AND `count_date` >= '{$_GET['minDate']}'";
        empty($_GET['maxDate']) || $sql .= " AND `count_date` <= '{$_GET['maxDate']}'";
        empty($_GET['skuName']) || $sql .= " AND `sku` = '{$_GET['skuName']}'";

        if( ($count = $this->request->param('id', false)) === false ) {                                                 //查询总长度
            $count = DB::query(Database::SELECT, 'SELECT COUNT(*) c' . $sql)
                ->execute('newerp')->get('c', 0);
        }

        $sql = "SELECT
            `sku`, `sale`, `count_date`
        {$sql}
        ORDER BY
            `count_date` DESC";

        empty($_GET['page']) && $_GET['page'] = 1;
        $_GET['page'] -= 1;
        $sql .= ' LIMIT ' . ($_GET['page'] * 10) . ', 10';

        $dataList = DB::query(Database::SELECT, $sql)->execute('newerp')->as_array();                                   //数据列表

        $pagination         = Pagination::factory(array(
          'items_per_page'  => 10, 
          'total_items'     => $count,
          'uri'             => '/salessearchshow/detailsData/' . $count,
        ));
?>

        <table class="table">
            <thead>
                <tr style=" background-color:#0099CC; color:#fff;">
                    <th> SKU </th>
                    <th> 商品名 </th>
                    <th> 销量 </th>
                    <th> 日期 </th>
                </tr>
            </thead>
            <tbody>
            <?php
            $cacheSku = array();
            foreach($dataList as &$v) {
                $cacheSku[$v['sku']] = &$v['sku'];
            }
            $temp = json_decode(skusystemapi::getMoreSkuCnname('("' . join('", "', $cacheSku)  . '")'), true);  //无可奈何的写法
            $cacheSku = array();
            if( $temp ) {
                foreach($temp as &$v) $cacheSku[$v['sku']] = &$v['name'];
            }

            foreach($dataList as $k => &$v) {
                echo "<tr>
                    <td style='background-color: #CCFFCC'> {$v['sku']} </td>
                    <td style='background-color: #CCFFCC'> {$cacheSku[$v['sku']]} </td>
                    <td style='background-color: #CCFFCC'> {$v['sale']} </td>
                    <td style='background-color: #CCFFCC'> {$v['count_date']} </td>
                </tr>";
            }
            ?>
            </tbody>
        </table>
<?php
        echo $pagination;
    }
}